<?php
/**
 *  分享统计
 */

class ShareCount extends APIBase {
	public $tag = "ShareCount";
	public $isLogin = true;
	public function before() {
		$this->initMysql();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
	    $playID = $this->uid;
		$data = array();
		//判断是否首次被邀请
		$row = $this->mysql->select("sharecount","*",array('sid'=>$playID));
		$sharereward = $this->cache_redis->keys("hsharereward:*");
		//首次被邀请的奖励
		$inviteward = array();
		foreach($sharereward as $k=>$val){
			$list = $this->cache_redis->hGetAll($val);
			if($list['id']==999){
			    $invitewardArr = json_decode($list['reward'],true);
				foreach($invitewardArr as $key=>$val){
					$prop = $this->cache_redis->hGetAll("prop:".$key);
					$inviteward[] = array('propname'=>$prop['name'],'count'=>$val);
				}
				$data['inviteward'][] = array('mid'=>$list['id'],'reward'=>$inviteward,'type'=>1);//type区别被邀请与邀请
			}
		}
		//判断是否能领取被邀请奖励1为可以，0为否
		if(!empty($row)){
			if(!$row[0]['status']){
				$data['status'] = 1;
			}else{
				$data['status'] = 0;
			}
		}else{
			$data['status'] = 0;
		}
		
		
		
		//判断邀请了多少用户
		$row0 = $this->mysql->select("sharecount","count(id) as num",array('topid'=>$playID));
		$R = $row0[0]['num'];
        $data['invite'] = $R;
		
		
		
		//分享奖励
	    $EmailList = $this->cache_redis->keys("hUserEmailList:$playID:*");
		sort($sharereward);
		$num = array();
		
		if(count($sharereward)>0){
			foreach($sharereward as $k=>$val){
				$flag = 0;
				$list = $this->cache_redis->hGetAll($val);
				if($list['id']!= 999){
				    /********奖励说明*****/
				    $reward = array();
				    $rewardArr = json_decode($list['reward'],true);
				    foreach($rewardArr as $key=>$val){
					    $prop = $this->cache_redis->hGetAll("prop:".$key);
					    $reward[] = array('propname'=>$prop['name'],'count'=>$val);
				    }
				    /********end*****/
				
				    //判断是否已经领取奖励
				    if(!empty($EmailList)){
						for($i=0;$i<count($EmailList);$i++){
							$mail = $this->cache_redis->hGetAll("hUserEmailList:$playID:".$i);
							if($mail['type']==4 && $mail['rewardid']==$list['id']){
								$flag = 1;
							}
						}  
				    }
					$data['list'][] = array('mid'=>$list['id'],'reward'=>$reward,'flag'=>$flag,'type'=>2,'sharenum'=>$list['sharenum']);
			    } 
			}
			
			//echo "<pre>";
			//print_r($data);exit;
			$this->returnData($data);
         
	    }else{
			$this->returnError(301,"数据异常");
		}
	}
    
	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
